package functions;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JOptionPane;

import gui.GuiLayout;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class MainProgram<E> {
	private Button btnAddPayment;
	private Button btnRemoveRecord;
	private GuiLayout gl;
	private PaymentData pd;
	private String pathToFile;
	public MainProgram(PaymentData pd, String pathToFile){
		this.pd = pd;
		this.pathToFile = pathToFile;
	}
	
	public void run(){
		Display display = new Display();
		this.gl = new GuiLayout(display);
		Shell shell = gl.getShell();
		this.btnAddPayment = gl.getBtnAddPayment();
		this.btnAddPayment.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				super.widgetSelected(e);
				addPayment();
			}
		});
		this.btnRemoveRecord = gl.getBtnRemoveRecord();
		this.btnRemoveRecord.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				super.widgetSelected(e);
				removeRecord();
			}
		});
		setupDisplayInformation();
		shell.open();
		shell.forceActive();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}
		display.dispose();
	}

	private void removeRecord() {
		// TODO Auto-generated method stub
		try{
			int index = Integer.parseInt(this.gl.getTxtIndex().getText());
			this.pd.removePayment(index - 1);
			
		}catch(Exception e){}
		setupDisplayInformation();
	}

	private void addPayment() {
		try{
			double amount = Double.parseDouble(gl.getTxtAmount().getText());
			int day = Integer.parseInt(gl.getTxtDay().getText());
			int month = Integer.parseInt(gl.getTxtMonth().getText());
			int year = Integer.parseInt(gl.getTxtYear().getText());
			pd.addPayment(month, day, year, amount);
			
		}catch(Exception e){
			
		}
		
		saveObject();
		printRecordSet();
		gl.getTxtDay().setText("");
		gl.getTxtMonth().setText("");
		gl.getTxtYear().setText("");
		gl.getTxtAmount().setText("");
		
		gl.getTxtTotalPaid().setText(pd.calculatePaymentSum()+"");
		gl.getTxtRemainingBalance().setText(pd.calculateRemainingBalance()+"");
		if(pd.calculateRemainingBalance() <= 0){
			JOptionPane.showMessageDialog(null, "Paid in full, time to celebrate!");
		}
		
	}
	
	private void saveObject(){
		
		try{
			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(this.pathToFile));
			out.writeObject(pd);
			out.close();
		}catch(Exception e){
			JOptionPane.showMessageDialog(null, "An error occurred while trying to save.");
			e.printStackTrace();
		}
	}
	
	private void printRecordSet(){
		gl.getTxtPaymentDisplay().setText("");
		ArrayList<SinglePayment> al = pd.getArray();
		Iterator<SinglePayment> it = al.iterator();
		int count = 1;
		String header = "index\t\tdate\t\tamount\n";
		//gl.getTxtPaymentDisplay().append(header);
		while(it.hasNext())
		{
			String value = count + " " + it.next().toFormattedString() + "\n";
			gl.getTxtPaymentDisplay().append(value);
			count++;
		}
	}

	private void setupDisplayInformation(){
		gl.getTxtTotalPaid().setText(pd.calculatePaymentSum()+"");
		printRecordSet();
		gl.getTxtMonth().forceFocus();
		gl.getTxtBeginningBalance().setText(pd.getBeginningBalance()+"");
		gl.getTxtRemainingBalance().setText(pd.calculateRemainingBalance()+"");
		gl.getTxtIndex().setText("");
	}
	
	
}
